<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">



  
  
    
    
  <script src="/dxl/lib/pace/pace.min.js?v=1.0.2"></script>
  <link href="/dxl/lib/pace/pace-theme-minimal.min.css?v=1.0.2" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/dxl/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/dxl/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/dxl/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/dxl/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/dxl/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/dxl/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/dxl/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="爬虫,">










<meta name="description" content="一、增量式式爬虫1.1什么是增量式爬虫1通过爬虫程序监测某网站数据更新的情况，以便可以爬取到该网站更新出的新数据。  1.2、如何进行增量式的爬取工作1234在发送请求之前判断这个URL是不是之前爬取过在解析内容后判断这部分内容是不是之前爬取过写入存储介质时判断内容是不是已经在介质中存在	分析：其实增量爬取的核心是去重， 至于去重的操作在哪个步骤起作用，只能说各有利弊。在我看来，前两种思路需要根据">
<meta name="keywords" content="爬虫">
<meta property="og:type" content="article">
<meta property="og:title" content="增量式式爬虫">
<meta property="og:url" content="http://yoursite.com/2019/09/20/【爬虫08】增量式爬虫/index.html">
<meta property="og:site_name" content="我的快乐时光">
<meta property="og:description" content="一、增量式式爬虫1.1什么是增量式爬虫1通过爬虫程序监测某网站数据更新的情况，以便可以爬取到该网站更新出的新数据。  1.2、如何进行增量式的爬取工作1234在发送请求之前判断这个URL是不是之前爬取过在解析内容后判断这部分内容是不是之前爬取过写入存储介质时判断内容是不是已经在介质中存在	分析：其实增量爬取的核心是去重， 至于去重的操作在哪个步骤起作用，只能说各有利弊。在我看来，前两种思路需要根据">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-09-20T12:30:03.311Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="增量式式爬虫">
<meta name="twitter:description" content="一、增量式式爬虫1.1什么是增量式爬虫1通过爬虫程序监测某网站数据更新的情况，以便可以爬取到该网站更新出的新数据。  1.2、如何进行增量式的爬取工作1234在发送请求之前判断这个URL是不是之前爬取过在解析内容后判断这部分内容是不是之前爬取过写入存储介质时判断内容是不是已经在介质中存在	分析：其实增量爬取的核心是去重， 至于去重的操作在哪个步骤起作用，只能说各有利弊。在我看来，前两种思路需要根据">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/dxl/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/09/20/【爬虫08】增量式爬虫/">





  <title>增量式式爬虫 | 我的快乐时光</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/dxl/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">我的快乐时光</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/dxl/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/dxl/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/dxl/2019/09/20/【爬虫08】增量式爬虫/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content>
      <meta itemprop="description" content>
      <meta itemprop="image" content="/dxl/images/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="我的快乐时光">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">增量式式爬虫</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-09-20T20:44:46+08:00">
                2019-09-20
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/dxl/categories/爬虫/" itemprop="url" rel="index">
                    <span itemprop="name">爬虫</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/dxl/2019/09/20/【爬虫08】增量式爬虫/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/dxl/2019/09/20/【爬虫08】增量式爬虫/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv">本文总阅读量
            <span class="busuanzi-value" id="busuanzi_value_page_pv"></span>次
            </span>
          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="一、增量式式爬虫"><a href="#一、增量式式爬虫" class="headerlink" title="一、增量式式爬虫"></a>一、增量式式爬虫</h3><h3 id="1-1什么是增量式爬虫"><a href="#1-1什么是增量式爬虫" class="headerlink" title="1.1什么是增量式爬虫"></a>1.1什么是增量式爬虫</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">通过爬虫程序监测某网站数据更新的情况，以便可以爬取到该网站更新出的新数据。</span><br></pre></td></tr></table></figure>

<h3 id="1-2、如何进行增量式的爬取工作"><a href="#1-2、如何进行增量式的爬取工作" class="headerlink" title="1.2、如何进行增量式的爬取工作"></a>1.2、如何进行增量式的爬取工作</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">在发送请求之前判断这个URL是不是之前爬取过</span><br><span class="line">在解析内容后判断这部分内容是不是之前爬取过</span><br><span class="line">写入存储介质时判断内容是不是已经在介质中存在</span><br><span class="line">	分析：其实增量爬取的核心是去重， 至于去重的操作在哪个步骤起作用，只能说各有利弊。在我看来，前两种思路需要根据实际情况取一个（也可能都用）。第一种思路适合不断有新页面出现的网站，比如说小说的新章节，每天的最新新闻等等；第二种思路则适合页面内容会更新的网站。第三个思路是相当于是最后的一道防线。这样做可以最大程度上达到去重的目的。</span><br></pre></td></tr></table></figure>

<h3 id="1-2去重方法"><a href="#1-2去重方法" class="headerlink" title="1.2去重方法"></a>1.2去重方法</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>、更具url进行判断</span><br><span class="line">	将爬取过程中产生的url进行存储，存储在redis的set中。当下次进行数据爬取时，首先对即将要发起的请求对应的url在存储的url的set中做判断，如果存在则不进行请求，否则才进行请求。</span><br><span class="line"><span class="number">2</span>、根据数据指纹判断：</span><br><span class="line">	对爬取到的网页内容进行唯一标识的制定，然后将该唯一表示存储至redis的set中。当下次爬取到网页数据的时候，在进行持久化存储之前，首先可以先判断该数据的唯一标识在redis的set中是否存在，在决定是否进行持久化存储</span><br></pre></td></tr></table></figure>

<p>2、参数说明</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">urlopen函数原型：urllib.request.urlopen(url, data=None, timeout=&lt;object object at 0x10af327d0&gt;, *, cafile=None, capath=None, cadefault=False, context=None)</span><br><span class="line"></span><br><span class="line">在上述案例中我们只使用了该函数中的第一个参数url。在日常开发中，我们能用的只有url和data这两个参数。</span><br><span class="line"></span><br><span class="line">url参数：指定向哪个url发起请求</span><br><span class="line">data参数：可以将post请求中携带的参数封装成字典的形式传递给该参数（暂时不需要理解，后期会讲）</span><br><span class="line"></span><br><span class="line">urlopen函数返回的响应对象，相关函数调用介绍：</span><br><span class="line">response.headers()：获取响应头信息</span><br><span class="line">response.getcode()：获取响应状态码</span><br><span class="line">response.geturl()：获取请求的url</span><br><span class="line">response.read()：获取响应中的数据值（字节类型）</span><br></pre></td></tr></table></figure>

<h3 id="二、项目案例"><a href="#二、项目案例" class="headerlink" title="二、项目案例"></a>二、项目案例</h3><h3 id="2-1、爬取4567tv网站中所有的电影详情数据"><a href="#2-1、爬取4567tv网站中所有的电影详情数据" class="headerlink" title="2.1、爬取4567tv网站中所有的电影详情数据"></a>2.1、爬取4567tv网站中所有的电影详情数据</h3><p>1、爬虫文件</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: utf-8 -*-</span></span><br><span class="line"><span class="keyword">import</span> scrapy</span><br><span class="line"><span class="keyword">from</span> scrapy.linkextractors <span class="keyword">import</span> LinkExtractor</span><br><span class="line"><span class="keyword">from</span> scrapy.spiders <span class="keyword">import</span> CrawlSpider, Rule</span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> redis <span class="keyword">import</span> Redis</span><br><span class="line"><span class="keyword">from</span> incrementPro.items <span class="keyword">import</span> IncrementproItem</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MovieSpider</span><span class="params">(CrawlSpider)</span>:</span></span><br><span class="line">    name = <span class="string">'movie'</span></span><br><span class="line">    <span class="comment"># allowed_domains = ['www.xxx.com']</span></span><br><span class="line">    start_urls = [<span class="string">'http://www.4567tv.tv/frim/index7-11.html'</span>]</span><br><span class="line"></span><br><span class="line">    rules = (</span><br><span class="line">        Rule(LinkExtractor(allow=<span class="string">r'/frim/index7-\d+\.html'</span>), callback=<span class="string">'parse_item'</span>, follow=<span class="literal">True</span>),</span><br><span class="line">    )</span><br><span class="line">    <span class="comment">#创建redis链接对象</span></span><br><span class="line">    conn = Redis(host=<span class="string">'127.0.0.1'</span>,port=<span class="number">6379</span>)</span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">parse_item</span><span class="params">(self, response)</span>:</span></span><br><span class="line">        li_list = response.xpath(<span class="string">'//li[@class="p1 m1"]'</span>)</span><br><span class="line">        <span class="keyword">for</span> li <span class="keyword">in</span> li_list:</span><br><span class="line">            <span class="comment">#获取详情页的url</span></span><br><span class="line">            detail_url = <span class="string">'http://www.4567tv.tv'</span>+li.xpath(<span class="string">'./a/@href'</span>).extract_first()</span><br><span class="line">            <span class="comment">#将详情页的url存入redis的set中</span></span><br><span class="line">            ex = self.conn.sadd(<span class="string">'urls'</span>,detail_url)</span><br><span class="line">            <span class="keyword">if</span> ex == <span class="number">1</span>:</span><br><span class="line">                print(<span class="string">'该url没有被爬取过，可以进行数据的爬取'</span>)</span><br><span class="line">                <span class="keyword">yield</span> scrapy.Request(url=detail_url,callback=self.parst_detail)</span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                print(<span class="string">'数据还没有更新，暂无新数据可爬取！'</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">#解析详情页中的电影名称和类型，进行持久化存储</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">parst_detail</span><span class="params">(self,response)</span>:</span></span><br><span class="line">        item = IncrementproItem()</span><br><span class="line">        item[<span class="string">'name'</span>] = response.xpath(<span class="string">'//dt[@class="name"]/text()'</span>).extract_first()</span><br><span class="line">        item[<span class="string">'kind'</span>] = response.xpath(<span class="string">'//div[@class="ct-c"]/dl/dt[4]//text()'</span>).extract()</span><br><span class="line">        item[<span class="string">'kind'</span>] = <span class="string">''</span>.join(item[<span class="string">'kind'</span>])</span><br><span class="line">        <span class="keyword">yield</span> item</span><br></pre></td></tr></table></figure>

<p>2、管道文件</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> redis <span class="keyword">import</span> Redis</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">IncrementproPipeline</span><span class="params">(object)</span>:</span></span><br><span class="line">    conn = <span class="literal">None</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">open_spider</span><span class="params">(self,spider)</span>:</span></span><br><span class="line">        self.conn = Redis(host=<span class="string">'127.0.0.1'</span>,port=<span class="number">6379</span>)</span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">process_item</span><span class="params">(self, item, spider)</span>:</span></span><br><span class="line">        dic = &#123;</span><br><span class="line">            <span class="string">'name'</span>:item[<span class="string">'name'</span>],</span><br><span class="line">            <span class="string">'kind'</span>:item[<span class="string">'kind'</span>]</span><br><span class="line">        &#125;</span><br><span class="line">        print(dic)</span><br><span class="line">        self.conn.lpush(<span class="string">'movieData'</span>,dic)</span><br><span class="line">        <span class="keyword">return</span> item</span><br></pre></td></tr></table></figure>

<h3 id="2-2爬取臭事百科中的段子和作者数据"><a href="#2-2爬取臭事百科中的段子和作者数据" class="headerlink" title="2.2爬取臭事百科中的段子和作者数据"></a>2.2爬取臭事百科中的段子和作者数据</h3><p>1、爬虫文件</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> scrapy</span><br><span class="line"><span class="keyword">from</span> scrapy.linkextractors <span class="keyword">import</span> LinkExtractor</span><br><span class="line"><span class="keyword">from</span> scrapy.spiders <span class="keyword">import</span> CrawlSpider, Rule</span><br><span class="line"><span class="keyword">from</span> incrementByDataPro.items <span class="keyword">import</span> IncrementbydataproItem</span><br><span class="line"><span class="keyword">from</span> redis <span class="keyword">import</span> Redis</span><br><span class="line"><span class="keyword">import</span> hashlib</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">QiubaiSpider</span><span class="params">(CrawlSpider)</span>:</span></span><br><span class="line">    name = <span class="string">'qiubai'</span></span><br><span class="line">    <span class="comment"># allowed_domains = ['www.xxx.com']</span></span><br><span class="line">    start_urls = [<span class="string">'https://www.qiushibaike.com/text/'</span>]</span><br><span class="line"></span><br><span class="line">    rules = (</span><br><span class="line">        Rule(LinkExtractor(allow=<span class="string">r'/text/page/\d+/'</span>), callback=<span class="string">'parse_item'</span>, follow=<span class="literal">True</span>),</span><br><span class="line">        Rule(LinkExtractor(allow=<span class="string">r'/text/$'</span>), callback=<span class="string">'parse_item'</span>, follow=<span class="literal">True</span>),</span><br><span class="line">    )</span><br><span class="line">    <span class="comment">#创建redis链接对象</span></span><br><span class="line">    conn = Redis(host=<span class="string">'127.0.0.1'</span>,port=<span class="number">6379</span>)</span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">parse_item</span><span class="params">(self, response)</span>:</span></span><br><span class="line">        div_list = response.xpath(<span class="string">'//div[@id="content-left"]/div'</span>)</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> div <span class="keyword">in</span> div_list:</span><br><span class="line">            item = IncrementbydataproItem()</span><br><span class="line">            item[<span class="string">'author'</span>] = div.xpath(<span class="string">'./div[1]/a[2]/h2/text() | ./div[1]/span[2]/h2/text()'</span>).extract_first()</span><br><span class="line">            item[<span class="string">'content'</span>] = div.xpath(<span class="string">'.//div[@class="content"]/span/text()'</span>).extract_first()</span><br><span class="line"></span><br><span class="line">            <span class="comment">#将解析到的数据值生成一个唯一的标识进行redis存储</span></span><br><span class="line">            source = item[<span class="string">'author'</span>]+item[<span class="string">'content'</span>]</span><br><span class="line">            source_id = hashlib.sha256(source.encode()).hexdigest()</span><br><span class="line">            <span class="comment">#将解析内容的唯一表示存储到redis的data_id中</span></span><br><span class="line">            ex = self.conn.sadd(<span class="string">'data_id'</span>,source_id)</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> ex == <span class="number">1</span>:</span><br><span class="line">                print(<span class="string">'该条数据没有爬取过，可以爬取......'</span>)</span><br><span class="line">                <span class="keyword">yield</span> item</span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                print(<span class="string">'该条数据已经爬取过了，不需要再次爬取了!!!'</span>)</span><br></pre></td></tr></table></figure>

<p>2、管道文件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">from redis import Redis</span><br><span class="line">class IncrementbydataproPipeline(object):</span><br><span class="line">    conn = None</span><br><span class="line"></span><br><span class="line">    def open_spider(self, spider):</span><br><span class="line">        self.conn = Redis(host=&apos;127.0.0.1&apos;, port=6379)</span><br><span class="line"></span><br><span class="line">    def process_item(self, item, spider):</span><br><span class="line">        dic = &#123;</span><br><span class="line">            &apos;author&apos;: item[&apos;author&apos;],</span><br><span class="line">            &apos;content&apos;: item[&apos;content&apos;]</span><br><span class="line">        &#125;</span><br><span class="line">        # print(dic)</span><br><span class="line">        self.conn.lpush(&apos;qiubaiData&apos;, dic)</span><br><span class="line">        return item</span><br></pre></td></tr></table></figure>


      
    </div>
    
    
    

    

    

    
      <div>
        <ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者：</strong>
    
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://yoursite.com/2019/09/20/【爬虫08】增量式爬虫/" title="增量式式爬虫">http://yoursite.com/2019/09/20/【爬虫08】增量式爬虫/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>
    本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/3.0/" rel="external nofollow" target="_blank">CC BY-NC-SA 3.0</a> 许可协议。转载请注明出处！
  </li>
</ul>

      </div>
    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/dxl/tags/爬虫/" rel="tag"># 爬虫</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/dxl/2019/09/20/【爬虫07】scrapy实现分布式爬虫/" rel="next" title="分布式爬虫">
                <i class="fa fa-chevron-left"></i> 分布式爬虫
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/dxl/2019/09/20/【0目录】爬虫学习目录/" rel="prev" title>
                 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/dxl/images/avatar.png" alt>
            
              <p class="site-author-name" itemprop="name"></p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/dxl/archives">
              
                  <span class="site-state-item-count">43</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/dxl/categories/index.html">
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/dxl/tags/index.html">
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-inline">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-sign-out"></i>
                我的友链
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="tencent://message/?Menu=yes&uin=1258517737&Site=QQ%E6%9E%81%E5%AE%A2&Service=300&sigT=45a1e5847943b64c6ff3990f8a9e644d2b31356cb0b4ac6b24663a3c8dd0f8aa12a595b1714f9d45/" title="申请坑位" target="_blank">申请坑位</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#一、增量式式爬虫"><span class="nav-number">1.</span> <span class="nav-text">一、增量式式爬虫</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1什么是增量式爬虫"><span class="nav-number">2.</span> <span class="nav-text">1.1什么是增量式爬虫</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2、如何进行增量式的爬取工作"><span class="nav-number">3.</span> <span class="nav-text">1.2、如何进行增量式的爬取工作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2去重方法"><span class="nav-number">4.</span> <span class="nav-text">1.2去重方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#二、项目案例"><span class="nav-number">5.</span> <span class="nav-text">二、项目案例</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1、爬取4567tv网站中所有的电影详情数据"><span class="nav-number">6.</span> <span class="nav-text">2.1、爬取4567tv网站中所有的电影详情数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2爬取臭事百科中的段子和作者数据"><span class="nav-number">7.</span> <span class="nav-text">2.2爬取臭事百科中的段子和作者数据</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
            <span id="scrollpercent"><span>0</span>%</span>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-hand-peace-o"></i>
  </span>
  <span class="author" itemprop="copyrightHolder"></span>

  
</div>









        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      本站访客数
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      人次
    </span>
  

  
    <span class="site-pv">
      本站总访问量
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>








        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/dxl/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/dxl/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/dxl/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/dxl/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/dxl/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/dxl/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/dxl/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/dxl/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/dxl/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/dxl/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/dxl/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/dxl/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/dxl/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: '13B0JGDuA6ttduN8AQaR8CzF-gzGzoHsz',
        appKey: 'I13r9r5mVgq4jQYpYy6V4gW3',
        placeholder: '欢迎大佬指点~~~',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  





  

  

  

  
  

  

  

  

</body>
</html>
